//
// Created by vista on 2017/12/20.
//


#include <vector>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <set>
using namespace std;

#ifndef NULL
#define NULL 0
#endif

struct ListNode {
    int val;
    ListNode *next;

    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    void swap(ListNode* l1,ListNode* l2){
        int swap_value = l1->val;
        l1->val = l2->val;
        l2->val = swap_value;
    }

    ListNode* swapPairs(ListNode* head) {
        if( head == NULL || head->next == NULL)
            return head;
        else{
            ListNode * ptr  = head ;
            do{
                swap(ptr,ptr->next);
                ptr=ptr->next;
                ptr=ptr->next;
            }while (ptr != NULL && ptr->next != NULL);
        }
        return head;
    }
};

int main(){

    Solution solution;

    __uint64_t timebegin =  clock_gettime_nsec_np(CLOCK_REALTIME);

    ListNode * listNode = new ListNode(1);
    ListNode * head = listNode;
    for (int i = 0; i < 4; ++i) {
        ListNode * new_node = new ListNode(i+2);
        listNode->next = new_node;
        listNode = new_node;
    }

    ListNode * list = solution.swapPairs(head);

    while (list != NULL) {
        std::cout << list->val << ",";
        list = list->next;
    }
    cout << endl;

    __uint64_t timeend =  clock_gettime_nsec_np(CLOCK_REALTIME);

    cout << (timeend - timebegin) / 1000000.0 << endl;

    return 0;

}